package cn.simon.service.Impl;

import cn.simon.exception.GlobalException;
import cn.simon.mapper.UserMapper;
import cn.simon.pojo.User;
import cn.simon.service.IUserService;
import cn.simon.util.CookieUtil;
import cn.simon.util.MD5Util;
import cn.simon.util.UUIDUtil;
import cn.simon.vo.LoginVo;
import cn.simon.vo.RespBean;
import cn.simon.vo.RespBeanEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 * simon-yian 开心
 * @author simon-yian

 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
     private UserMapper userMapper;
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
       /* if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //邮箱验证格式
        if(!ValidatorUtil.isMobile(mobile)){
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }*/

        User user  =  userMapper.selectById(mobile);
        if(null == user){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw  new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //判断密码是否正确（第二次对密码加密，使用Salt）
        if(!MD5Util.formPassToDBPass(password, user.getSalt()).equals(user.getPassword())){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw  new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        String ticket = UUIDUtil.uuid();
//        request.getSession().setAttribute(ticket,user);
        redisTemplate.opsForValue().set("user:"+ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return RespBean.success(ticket);
    }

    /***
     * 获取cookic中用户信息
     * @param userTicket
     * @return
     */
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {

        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
       if(user != null){
           CookieUtil.setCookie(
                   request,response,"userTicket",userTicket
           );
       }

        return user;
    }

/**
 * @功能描述:
 * @Param:
 * @Return:
 * @Author suxiangshan
 * @Date 2022/11/1 21:13
 */
    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {

        User user = getUserByCookie(userTicket,request,response);
        if(user == null){
            return RespBean.error(RespBeanEnum.PHONE_NOTEXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSalt()));
        int i = userMapper.update(user,new QueryWrapper<>());
        if(i == 1){
            //删除缓存中的数据
            redisTemplate.delete("user:"+userTicket);
            return RespBean.success(RespBeanEnum.SUCCESS);
        }

        return  RespBean.error(RespBeanEnum.UPDATE_PHONE_FAIL);
    }
}
